<!--

    Copyright (C) 2015 The Gravitee team (http://gravitee.io)

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

-->
<div class="entrypoints">
  <form [formGroup]="formGroup" *ngIf="formGroup" (ngSubmit)="onSaveChanges()">
    <mat-card *ngIf="apiExistingPaths?.length > 0">
      <mat-card-content>
        <div class="entrypoints__context-path">
          <div class="entrypoints__context-path__title">
            <span class="mat-body-strong">Entrypoint context-paths</span>

            <button
              *ngIf="canUpdate"
              id="switchListenerType"
              mat-button
              type="button"
              (click)="switchEntrypointsMode()"
              [disabled]="this.isReadOnly || (this.enableVirtualHost && this.domainRestrictions.length > 1)"
            >
              <mat-icon [svgIcon]="enableVirtualHost ? 'gio:cancel' : 'gio:check'"></mat-icon>
              {{ enableVirtualHost ? 'Disable virtual hosts' : 'Enable virtual hosts' }}
            </button>
          </div>
          <gio-form-listeners-context-path
            *ngIf="!this.enableVirtualHost"
            [formControl]="pathsFormControl"
            [apiId]="apiId"
          ></gio-form-listeners-context-path>

          <gio-form-listeners-virtual-host
            *ngIf="this.enableVirtualHost"
            [formControl]="pathsFormControl"
            [apiId]="apiId"
            [domainRestrictions]="domainRestrictions"
          ></gio-form-listeners-virtual-host>
        </div>
        <div class="entrypoints__footer" *ngIf="canUpdate">
          <button
            mat-flat-button
            color="primary"
            type="submit"
            [disabled]="formGroup.pristine || formGroup.invalid || dataSource.length === 0"
          >
            Save changes
          </button>
          <button mat-stroked-button type="button" (click)="onReset()" [disabled]="!formGroup.dirty">Reset</button>
        </div>
      </mat-card-content>
    </mat-card>
    <mat-card *ngIf="apiExistingHosts?.length > 0">
      <mat-card-content>
        <div class="entrypoints__host">
          <div class="entrypoints__host__title">
            <span class="mat-body-strong">Entrypoint hosts</span>
          </div>
          <gio-form-listeners-tcp-hosts [formControl]="hostsFormControl" [apiId]="apiId"></gio-form-listeners-tcp-hosts>
        </div>
        <div class="entrypoints__footer" *ngIf="canUpdate">
          <button
            mat-flat-button
            color="primary"
            type="submit"
            [disabled]="formGroup.pristine || formGroup.invalid || dataSource.length === 0"
          >
            Save changes
          </button>
          <button mat-stroked-button type="button" (click)="onReset()" [disabled]="!formGroup.dirty">Reset</button>
        </div>
      </mat-card-content>
    </mat-card>
  </form>
  <mat-card *ngIf="api?.type === 'MESSAGE'">
    <mat-card-content>
      <div class="entrypoints__type">
        <span class="mat-body-strong">Entrypoint types</span>
        <table mat-table [dataSource]="dataSource" class="entrypoints__type__table gio-table-light" aria-label="entrypoints">
          <ng-container matColumnDef="type">
            <th mat-header-cell *matHeaderCellDef>Entrypoint type</th>
            <td mat-cell *matCellDef="let element">
              <div class="entrypoints__type__table__type"><mat-icon [svgIcon]="element.icon"></mat-icon> {{ element.type }}</div>
            </td>
          </ng-container>
          <ng-container matColumnDef="qos">
            <th mat-header-cell *matHeaderCellDef>Quality of Service</th>
            <td mat-cell *matCellDef="let element">
              <div class="entrypoints__type__table__type">{{ element.qos }}</div>
            </td>
          </ng-container>
          <ng-container matColumnDef="actions">
            <th mat-header-cell *matHeaderCellDef></th>
            <td mat-cell *matCellDef="let element" class="entrypoints__type__table__actions">
              <ng-container *ngIf="canUpdate">
                <button type="button" mat-icon-button aria-label="Edit" [routerLink]="[element.id]">
                  <mat-icon svgIcon="gio:edit-pencil" matTooltip="Edit"> </mat-icon>
                </button>
                <button type="button" [disabled]="dataSource.length <= 1" mat-icon-button aria-label="Delete" (click)="onDelete(element)">
                  <mat-icon
                    svgIcon="gio:trash"
                    [matTooltip]="dataSource.length <= 1 ? 'At least one entrypoint is required' : 'Delete'"
                  ></mat-icon>
                </button>
              </ng-container>
            </td>
          </ng-container>

          <tr mat-header-row *matHeaderRowDef="displayedColumns" class="entrypoints__type__table__header"></tr>
          <tr mat-row *matRowDef="let row; columns: displayedColumns"></tr>
        </table>

        <div *ngIf="shouldUpgrade">
          <gio-license-banner
            [license]="license$ | async"
            [isOEM]="isOEM$ | async"
            (onRequestUpgrade)="onRequestUpgrade()"
          ></gio-license-banner>
        </div>

        <button
          *ngIf="canUpdate"
          mat-stroked-button
          type="button"
          [disabled]="entrypointAvailableForAdd.length < 1"
          (click)="addNewEntrypoint()"
        >
          Add an entrypoint
        </button>
      </div>
    </mat-card-content>
  </mat-card>
</div>
